package com.zhongxi.guoyuan.fragment.beautifulStore;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.CommentAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.comment.CommentData;
import com.zhongxi.guoyuan.entity.comment.CommentResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.widget.xlistview.XListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/8.
 */

@SuppressLint("ValidFragment")
public class BeautifulStoreCommentFragment extends BaseFragment {

    private XListView lvList;
    private CommentAdapter adapter;
    private List<CommentData> data = new ArrayList<>();
    private String id;
    private String ctype = "0";
    private boolean isLoadMore;

    @SuppressLint("ValidFragment")
    public BeautifulStoreCommentFragment(String id) {
        super();
        this.id = id;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            ctype = getArguments().getString("ctype");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_comment, container, false);
    }

    @Override
    protected void initView() {
        super.initView();
        lvList = (XListView) getView().findViewById(R.id.lvList);
        lvList.setPullRefreshEnable(true);
        lvList.setPullLoadEnable(true);
    }

    @Override
    protected void initData() {
        super.initData();    //  ctype=0 项目评论   ctype=1  店铺评论
        adapter = new CommentAdapter(context, data);
        lvList.setAdapter(adapter);
        pinglun();
    }

    private void pinglun() {
        if ("1".equals(ctype)) {
            getServer(Url.COMMENTS, null, "&page=" + pageNum + "&ctype=" + 1 + "&sid=" + id, false, null, "get");
        } else {
            getServer(Url.COMMENTS, null, "&page=" + pageNum + "&ctype=" + 0 + "&sid=" + id, false, null, "get");
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        lvList.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                pageNum = 1;
                pinglun();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                pageNum++;
                pinglun();
            }
        });
    }


    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            CommentResult result = gson.fromJson(response, CommentResult.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if (isLoadMore) {
                if (result.getComments() == null || result.getComments().size() == 0) {
                    showShortToast("没有加载更多了");
                    isFinish();
                    return;
                }
            }
            if (pageNum == 1) {
                data.clear();
            }
            if (result.getComments().size() > 0) {
                data.addAll(result.getComments());
            }
            try {
                JSONObject obj = new JSONObject(response);
                JSONObject userObj = obj.getJSONObject("user");
                if (null != userObj && !"".equals(userObj)) {
                    adapter.setUser(userObj);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            adapter.notifyDataSetChanged();
            isFinish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        }
    }

    private void isFinish() {
        lvList.stopRefresh();
        lvList.stopLoadMore();
        lvList.setRefreshTime("刚刚");
    }
}
